package com.qf.commerce.controller;

import com.github.pagehelper.PageInfo;
import com.qf.commerce.common.dto.DealerDto;
import com.qf.commerce.common.vo.DealerVo;
import com.qf.commerce.resp.ResponseResult;
import com.qf.commerce.service.DealerService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/dealer")
public class DealerController {

    @Resource
    private DealerService dealerService;

    /**
     * 展示所有经销商
     *
     * @param page page
     * @param size size
     * @return com.qf.commerce.resp.ResponseResult<com.github.pagehelper.PageInfo < com.qf.commerce.common.vo.DealerVo>>
     * @author 358
     * @since JDK 1.8
     */
    @GetMapping("/list")
    public ResponseResult<PageInfo<DealerVo>> list(Integer page, Integer size) {
        try {
            return ResponseResult.success(dealerService.selectListPage(page, size));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 用户申请成为经销商
     *
     * @param dealerDto dealerDto
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PostMapping("/apply")
    public ResponseResult<Integer> applyDealer(@RequestBody DealerDto dealerDto) {
        try {
            return ResponseResult.success(dealerService.applyDealer(dealerDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 同意经销商申请
     *
     * @param dealerId dealerId
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PutMapping("/apply/agree")
    public ResponseResult<Integer> agreeDealerApply(Integer dealerId) {
        try {
            return ResponseResult.success(dealerService.agreeDealerApply(dealerId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 拒绝经销商申请
     *
     * @param dealerId dealerId
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PutMapping("/apply/disagree")
    public ResponseResult<Integer> disagreeDealerApply(Integer dealerId) {
        try {
            return ResponseResult.success(dealerService.disagreeDealerApply(dealerId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 封禁经销商
     *
     * @param dealerId dealerId
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @DeleteMapping("/delete")
    public ResponseResult<Integer> deleteDealer(Integer dealerId) {
        try {
            return ResponseResult.success(dealerService.deleteById(dealerId));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }

    /**
     * 更改经销商申请提交信息
     *
     * @param dealerDto dealerDto
     * @return com.qf.commerce.resp.ResponseResult<java.lang.Integer>
     * @author 358
     * @since JDK 1.8
     */
    @PutMapping("/apply/update")
    public ResponseResult<Integer> dealerApplyUpdate(@RequestBody DealerDto dealerDto) {
        try {
            return ResponseResult.success(dealerService.updateByPrimaryKeySelective(dealerDto));
        } catch (Exception e) {
            return ResponseResult.fail();
        }
    }
}
